﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Wpf_Examples.Models;

namespace Wpf_Examples.ViewModels
{
    public class PageBarViewModel:ObservableObject
    {
        #region 字段
        public ICollectionView ViewSource { set; get; }
        int showDataCount = 0;

        #endregion

        #region 属性
        /// <summary>
        /// 分页页序号
        /// </summary>
        private int pageIndex = 1;
        public int PageIndex
        {
            get { return pageIndex; }
            set { SetProperty(ref pageIndex, value); RefreshView(); }
        }
        /// <summary>
        /// 分页每页显示数据条数
        /// </summary>
        private int pageSize;
        public int PageSize
        {
            get { return pageSize; }
            set { SetProperty(ref pageSize, value); RefreshView(); }
        }
        /// <summary>
        /// 总显示数据条数
        /// </summary>
        private int totalCount = 0;
        public int TotalCount
        {
            get { return totalCount; }
            set { SetProperty(ref totalCount, value); }
        }
        /// <summary>
        /// 需要显示的数据集合
        /// </summary>
        private ObservableCollection<DataInfoListModel> dataInfoList = new ObservableCollection<DataInfoListModel>();
        public ObservableCollection<DataInfoListModel> DataInfoList
        {
            get { return dataInfoList; }
            set { SetProperty(ref dataInfoList, value); }
        }



        #endregion

        public PageBarViewModel()
        {
            Random random = new Random();
            for (int i = 0; i < 30; i++)
            {
                DataInfoListModel dataInfoListModel = new DataInfoListModel();
                dataInfoListModel.Order = (i + 1);
                dataInfoListModel.Id = (i + 1); 
                dataInfoListModel.Name = "测试"+(i+1)+"号";
                dataInfoListModel.Gender = "男";
                dataInfoListModel.Age = random.Next(10,80);
                dataInfoListModel.Phone = "17455861459";
                dataInfoListModel.Email = "1453849257@qq.com";
                DataInfoList.Add(dataInfoListModel);
            }
            TotalCount = DataInfoList.Count;
            ViewSource = CollectionViewSource.GetDefaultView(DataInfoList);
            ViewSource.Filter = new Predicate<object>(OnFilterMovie);
        }


        private void RefreshView()
        {
            showDataCount = 0;
            ViewSource.Refresh();
        }

        /// <summary>
        /// 数据过滤器
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private bool OnFilterMovie(object obj)
        {
            if (DataInfoList.Count == 0) return false;
            if (showDataCount >= pageSize) return false;
            if (!(obj is DataInfoListModel info)) return false;
            bool flag = UserControlLib.Actions.PageBarAction.GetDataByPage(pageSize, PageIndex, DataInfoList.ToList(), info);
            if (flag) showDataCount++;
            return flag;
        }

    }
}
